﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DSS_Simplex
{
    public delegate void SearchContextChangedHandler(List<field> fields);

    public struct field
    {
        public string Field;
        public string FriendlyName;
        public string Value;
    }

    class Mesin
    {
        //for reporting
        public static List<double[,]> ListRepValue;
        public static List<int> ListRepIndex;

        public static void CariMax(double[,] table)
        {
            int i, j = 0, s, n;
            double calc;
            int baris, kolom;
            double tanda;
            double min, max;
            int flag = 0;

            baris = table.GetLength(0);
            kolom = table.GetLength(1);
            double[] tanda2 = new double[baris - 1];

            //buat list report
            ListRepValue = new List<double[,]>();
            ListRepIndex = new List<int>();

            for (i = 1; i < kolom; i++)
            {
                if (table[0, i] >= 0)
                    flag = 0;
                if (table[0, i] < 0)
                {
                    flag = 1;
                    break;
                }
            }
            while (flag == 1)
            {
                max = table[0, 1];
                s = 1;

                Console.WriteLine("%%%%%%%%%%%%%%%%%%%");

                for (i = 1; i < kolom; i++)
                {
                    if (max > table[0, i])
                    {
                        max = table[0, i];
                        s = i;
                    }
                }
                Console.Write("titik maksimum: ");
                Console.WriteLine(max);
                min = table[1, 0] / table[1, s];
                j = 1;

                for (i = 2; i < baris; i++)
                    if (table[i, s] != 0)
                    {
                        tanda2[i - 1] = table[i, 0] / table[i, s];

                        if (tanda2[i - 1] > 0 && ((tanda2[i - 1] < min) || (min <= 0)))
                        {
                            min = tanda2[i - 1];
                            j = i;
                        }

                    }
                Console.Write("titik rasio: ");
                Console.WriteLine(min);
                tanda = table[j, s];
                for (i = 0; i < kolom; i++)
                {
                    table[j, i] /= tanda;
                }
                for (i = 0; i < baris; i++)
                {
                    if (i != j)
                    {
                        calc = table[i, s] / table[j, s];
                        for (n = 0; n < kolom; n++)
                        {
                            table[i, n] -= table[j, n] * calc;
                        }
                    }
                }

                //add listrepindex
                ListRepIndex.Add(s);
                //add new table into list
                Console.WriteLine("sebelummmmmmmmmmmmmmm "+ListRepValue.Count());
                ListRepValue.Add(table);
                Console.WriteLine("sudahhhhhhhhhhhhhhhhh " + ListRepValue.Count());
                
                Console.WriteLine("ini list : ");

                for (int ii = 0; ii < ListRepValue.ElementAt(0).GetLength(0); ii++)
                {
                    for (int jj = 0; jj < ListRepValue.ElementAt(0).GetLength(1); jj++)
                    {
                        Console.Write("" + ListRepValue.ElementAt(0)[ii, jj]);
                        
                    }
                }
                Console.WriteLine("iterasi:");
                for (i = 0; i < baris; i++)
                {
                    for (j = 0; j < kolom; j++)
                    {
                        Console.Write(table[i, j]);
                        Console.Write(" ");
                    }
                    Console.WriteLine();
                }
                


                Console.WriteLine("end iteration.");

                for (i = 1; i < kolom; i++)
                {
                    if (table[0, i] >= 0)
                        flag = 0;
                    if (table[0, i] < 0)
                    {
                        flag = 1;
                        break;
                    }
                }

                /*List<int> list = new List<int>();
                list.Add(999);
                int xx = list.ElementAt(0);
            
                double[,] copytable = listt.ElementAt(0);
                Console.WriteLine("" + copytable);
                
                for (int ii = 0; ii < listt.Count(); ii++)
                {
                    for (int jj = 0; jj < listt.ElementAt(ii).Length; jj++)
                        for (int kk = 0; kk < listt.ElementAt(ii)[jj,ii]; kk++)
                        Console.Write(""+listt.ElementAt(i)[jj,0]);
                }
                */
                
            }
        }

        public static void CariMin(double[,] table)
        {
            int i, j = 0, s, n;
            double calc;
            int baris, kolom;
            double tanda;
            double min, max;
            int flag = 0;

            baris = table.GetLength(0);
            kolom = table.GetLength(1);
            double[] tanda2 = new double[baris - 1];

            //buat list
            ListRepValue = new List<double[,]>();

            for (i = 1; i < kolom; i++)
            {
                if (table[0, i] <= 0)
                    flag = 0;
                if (table[0, i] > 0)
                {
                    flag = 1;
                    break;
                }
            }
            while (flag == 1)
            {
                max = table[0, 1];
                s = 1;

                Console.WriteLine("%%%%%%%%%%%%%%%%%%%");

                for (i = 1; i < kolom; i++)
                {
                    if (max < table[0, i])
                    {
                        max = table[0, i];
                        s = i;
                    }
                }
                Console.Write("titik maksimum: ");
                Console.WriteLine(max);
                min = table[1, 0] / table[1, s];
                j = 1;
                
                for (i = 2; i < baris; i++)
                    if (table[i, s] != 0)
                    {
                        tanda2[i - 1] = table[i, 0] / table[i, s];
                        
                        if (tanda2[i-1] > 0 && ((tanda2[i - 1] < min) || (min <= 0) ))
                        {
                            min = tanda2[i - 1];
                            j = i;
                        }

                    }
                Console.Write("titik rasio: ");
                Console.WriteLine(min);
                tanda = table[j, s];
                for (i = 0; i < kolom; i++)
                {
                    table[j, i] /= tanda;
                }
                for (i = 0; i < baris; i++)
                {
                    if (i != j)
                    {
                        calc = table[i, s] / table[j, s];
                        for (n = 0; n < kolom; n++)
                        {
                            table[i, n] -= table[j, n] * calc;
                        }
                    }
                }
                Console.WriteLine("iterasi:");
                for (i = 0; i < baris; i++)
                {
                    for (j = 0; j < kolom; j++)
                    {
                        Console.Write(table[i, j]);
                        Console.Write(" ");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("end iteration.");
                //add new table into list
                Console.WriteLine("sebelummmmmmmmmmmmmmm " + ListRepValue.Count());
                //ListRepValue.Add(table);
                ListRepValue.Add(new double[table.GetLength(0), table.GetLength(1)]);
                Array.Copy(table, ListRepValue.ElementAt(ListRepValue.Count() - 1), table.Length);
                Console.WriteLine("sudahhhhhhhhhhhhhhhhh " + ListRepValue.Count());
                Console.WriteLine("ini list : ");

                for (int ii = 0; ii < ListRepValue.ElementAt(ListRepValue.Count()-1).GetLength(0); ii++)
                {
                    for (int jj = 0; jj < ListRepValue.ElementAt(ListRepValue.Count()-1).GetLength(1); jj++)
                    {
                        Console.Write("" + ListRepValue.ElementAt(ListRepValue.Count()-1)[ii, jj]);

                    }
                }
                Console.WriteLine("iterasi:");

                for (i = 1; i < kolom; i++)
                {
                    if (table[0, i] <= 0)
                        flag = 0;
                    if (table[0, i] > 0)
                    {
                        flag = 1;
                        break;
                    }
                }
            }

            Console.WriteLine("cek semua list : ");

            for (int ii = 0; ii < ListRepValue.Count(); ii++)
            {
                for (int jj = 0; jj < ListRepValue.ElementAt(ii).GetLength(0); jj++)
                {
                    for (int kk = 0; kk < ListRepValue.ElementAt(ii).GetLength(1); kk++)
                        Console.Write(" " + ListRepValue.ElementAt(ii)[jj, kk]);
                    Console.WriteLine(" -------------------------------------------");
                }
                Console.WriteLine();
            }

        }
        
        public static void InnerProduct(ref double[,] Mtable, int A1, int S2, int A2, double Mval)
        {
            int i, j;
            int VarAll = S2 + A2 + A1;
            int TCol = Mtable.GetLength(1);
            double[] VectorA = new double[VarAll];
            double[] coefVar = new double[TCol];
            double sum = 0;
            MessageBox.Show("tcol :" + TCol);

            for (i = 0; i < S2; i++)
            {
                VectorA[i] = 0;
            }
            MessageBox.Show("1");
            for (i = S2; i < S2 + A1; i++)
            {
                VectorA[i] = Mval;
            }
            MessageBox.Show("2");
            for (i = S2 + A1; i < VarAll; i++)
            {
                VectorA[i] = Mval;
            }
            MessageBox.Show("3");
            //sum product
            for (j = 0; j < TCol; j++)
            {
                for (i = 0; i < VectorA.Length; i++)
                {
                    sum += VectorA[i] * Mtable[i + 1, j];
                    MessageBox.Show("4");
                }
                coefVar[j] = sum - Mtable[0, j];
                sum = 0;
                MessageBox.Show("5");
            }
            
            //ganti dgn var baru
            for (i = 0; i < TCol; i++)
            {
                Mtable[0, i] = coefVar[i];
            }

        }

        public static string[] ArrRep;
        public static string[] ArrRepHeader;
        public static void RunComputation(bool toGetMax, DataGridView dataGridView1, DataGridView dataGridView2, DataGridView dataGridView4)
        {
            int i;
            int j = 0;

            int varinit = dataGridView1.RowCount - 1;
            int constraint = dataGridView2.RowCount - 1;
            int vartotal = varinit + constraint;

            //max,min
            double[,] coef = new double[constraint, vartotal];
            double[,] bcoef = new double[constraint, 1];
            double[] ccoef = new double[vartotal];
            double[,] table = new double[constraint + 1, vartotal + 1];

            //dual
            double[,] coefd = new double[constraint, varinit];
            double[,] tcoef = new double[varinit, vartotal];
            double[,] tbcoef = new double[varinit, 1];
            double[] tccoef = new double[vartotal];
            double[,] ttable = new double[varinit + 1, vartotal + 1];
            int tbrs;
            int tklm;

            //bigM
            double[] arrMin = new double[constraint];
            double[] arrMax = new double[constraint];
            double[] arrEq = new double[constraint];
            int S1, S2, A1, A2;

            #region kondisi

            #region arr report iteration
            //isi array utk report
            ArrRep = new string[constraint];
            ArrRepHeader = new string[varinit];

            for (i = 0; i < dataGridView1.RowCount - 1; i++)
            {
                ArrRepHeader[i] = dataGridView1.Rows[i].Cells[0].Value + "";
            }

            #endregion

            //kondisi
            int simMin = 0, simMax = 0, simEq = 0;
            int countArr = 0;
            for (i = 0; i < dataGridView2.RowCount - 1; i++)
            {
                if (Convert.ToString(dataGridView2.Rows[i].Cells[2].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) == string.Empty)
                {
                    simMax++;
                    ArrRep[countArr] = dataGridView2.Rows[i].Cells[0].Value +"";
                    countArr++;
                }
            }
            for (i = 0; i < dataGridView2.RowCount - 1; i++)
            {
                if (Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[2].Value) == string.Empty)
                {
                    simMin++;
                    ArrRep[countArr] = dataGridView2.Rows[i].Cells[0].Value + "";
                    countArr++;
                }
            }
            for (i = 0; i < dataGridView2.RowCount - 1; i++)
            {
                if (Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[2].Value) != string.Empty)
                {
                    simEq++;
                    ArrRep[countArr] = dataGridView2.Rows[i].Cells[0].Value + "";
                    countArr++;
                }
            }
            #endregion

            #region Button Maksimasi
            //kondisi maksimum
            if (toGetMax)
            {

                #region Simplex Max

                if (simMax == constraint && simMin == 0 && simEq == 0)
                {
                    //matriks awal
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            coef[i, j] = double.Parse(dataGridView4.Rows[i].Cells[j + 1].Value + "");
                            Console.Write(coef[i, j]);
                        }
                        Console.WriteLine(" awal");
                    }
                    //nilai awal S
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = varinit; j < vartotal; j++)
                        {
                            coef[i, j] = 0;
                            Console.Write(coef[i, j]);
                        }
                        Console.WriteLine(" tambahan variabel");
                    }

                    //buat variabel bantu
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = varinit; j < vartotal; j++)
                        {
                            coef[i, varinit + i] = 1;
                            Console.Write(coef[i, j]);
                        }
                        Console.WriteLine(" tambah 1");
                    }

                    //nilai kanan jika smua constraint memiliki batas maksimum
                    for (i = 0; i < constraint; i++)
                    {
                        bcoef[i, 0] = double.Parse(dataGridView2.Rows[i].Cells[2].Value + "");

                    }
                    //koefisien fungsi tujuan
                    for (j = 0; j < varinit; j++)
                    {
                        ccoef[j] = double.Parse(dataGridView1.Rows[j].Cells[1].Value + "");
                    }
                    for (j = varinit; j < vartotal; j++)
                    {
                        ccoef[j] = 0;
                    }
                    //menghasilkan tabel
                    table[0, 0] = 0;
                    for (i = 1; i < vartotal + 1; i++)
                    {
                        table[0, i] = ccoef[i - 1];
                    }
                    for (i = 1; i < constraint + 1; i++)
                    {
                        table[i, 0] = bcoef[i - 1, 0];
                    }
                    for (i = 1; i < constraint + 1; i++)
                    {
                        for (j = 1; j < vartotal + 1; j++)
                        {
                            table[i, j] = coef[i - 1, j - 1];
                        }
                    }
                    //matriks awal
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < vartotal; j++)
                        {
                            Console.Write(table[i, j]);
                        }
                        Console.WriteLine("tabel");
                    }


                    Mesin.CariMin(table);
                    //hasil
                    for (i = 0; i < constraint + 1; i++)
                    {
                        for (j = 0; j < vartotal + 1; j++)
                            Console.Write("{0,5}  ", table[i, j].ToString());
                        Console.WriteLine();
                    }
                    Console.WriteLine("{0} nilai fungsi objective", -table[0, 0]);

                }
                #endregion

                #region Dual Max
                else if (simMin == constraint && simMax == 0 && simEq == 0)
                {

                    //matriks awal
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            coefd[i, j] = double.Parse(dataGridView4.Rows[i].Cells[j + 1].Value + "");
                            Console.Write(coefd[i, j]);
                        }
                        Console.WriteLine(" matrik awal");
                    }
                    //transpose
                    tklm = coefd.GetLength(0);
                    Console.WriteLine(tklm);
                    tbrs = coefd.GetLength(1);
                    Console.WriteLine(tbrs);

                    //transpose matrikx
                    for (i = 0; i < tklm; i++)
                    {
                        for (j = 0; j < tbrs; j++)
                        {
                            tcoef[j, i] = coefd[i, j];
                        }
                    }
                    Console.WriteLine(" matriks transpose");

                    //tambah coef dg 0
                    for (i = 0; i < varinit; i++)
                        for (j = constraint; j < vartotal; j++)
                        {
                            tcoef[i, j] = 0;

                        }
                    //var s
                    for (i = 0; i < varinit; i++)
                    {
                        for (j = constraint; j < vartotal; j++)
                        {
                            tcoef[i, constraint + i] = 1;
                        }
                    }

                    //matriks kendala
                    for (i = 0; i < varinit; i++)
                    {
                        for (j = 0; j < vartotal; j++)
                        {
                            Console.Write(tcoef[i, j]);
                        }
                        Console.WriteLine();
                    }
                    //nilai kanan diambil dr fungsi kendala
                    for (i = 0; i < varinit; i++)
                    {
                        tbcoef[i, 0] = double.Parse(dataGridView1.Rows[i].Cells[1].Value + "");
                    }

                    //koefisien fungsi tujuan
                    for (j = 0; j < constraint; j++)
                    {
                        tccoef[j] = double.Parse(dataGridView2.Rows[j].Cells[1].Value + "");
                    }
                    for (j = constraint; j < vartotal; j++)
                    {
                        tccoef[j] = 0;
                    }

                    //menghasilkan tabel
                    ttable[0, 0] = 0;
                    for (i = 1; i < vartotal + 1; i++)
                    {
                        ttable[0, i] = tccoef[i - 1];
                    }
                    for (i = 1; i < varinit + 1; i++)
                    {
                        ttable[i, 0] = tbcoef[i - 1, 0];
                    }
                    for (i = 1; i < varinit + 1; i++)
                    {
                        for (j = 1; j < vartotal + 1; j++)
                        {
                            ttable[i, j] = tcoef[i - 1, j - 1];

                        }

                    }
                    //tabel awal
                    for (i = 0; i < varinit + 1; i++)
                    {
                        for (j = 0; j < vartotal + 1; j++)
                        {
                            Console.Write(ttable[i, j]);
                        }
                        Console.WriteLine(" hasil");
                    }

                    Mesin.CariMax(ttable);
                    //hasil
                    for (i = 0; i < varinit + 1; i++)
                    {
                        for (j = 0; j < vartotal + 1; j++)
                            Console.Write("{0,5}  ", ttable[i, j].ToString());
                        Console.WriteLine();
                    }
                    Console.WriteLine("{0} nilai fungsi objective", ttable[0, 0]);
                }
                #endregion

                #region MMax
                else
                {
                    double Mval;
                    Mval = 0;
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            coef[i, j] = double.Parse(dataGridView4.Rows[i].Cells[j + 1].Value + "");
                            Console.Write(coef[i, j]);

                        }
                        Console.WriteLine(" awal");
                    }
                    for (i = 0; i < varinit; i++)
                    {

                    }
                    Console.WriteLine("koefisien awal c");
                    //isi array dgn 0
                    for (i = 0; i < constraint; i++)
                    {
                        arrMin[i] = 0;
                        arrMax[i] = 0;
                        arrEq[i] = 0;
                    }
                    Console.WriteLine("isi 0");

                    //isi dgn nilai DGview2
                    for (i = 0; i < constraint; i++)
                    {
                        
                        if (Convert.ToString(dataGridView2.Rows[i].Cells[2].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) == string.Empty)
                        {
                            arrMax[i] = double.Parse(dataGridView2.Rows[i].Cells[2].Value + "");
                            Console.Write("max ");
                            Console.Write(arrMax[i]);
                        }
                        if (Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[2].Value) == string.Empty)
                        {
                            arrMin[i] = double.Parse(dataGridView2.Rows[i].Cells[1].Value + "");
                            Console.Write("min ");
                            Console.Write(arrMin[i]);

                        }
                        if (Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[2].Value) != string.Empty)
                        {
                            arrEq[i] = double.Parse(dataGridView2.Rows[i].Cells[2].Value + "");
                            Console.Write("Eq ");
                            Console.Write(arrEq[i]);
                        }
                    }
                    //count s1,s2,A1
                    S1 = 0;
                    A1 = 0;
                    for (i = 0; i < arrMin.Length; i++)
                    {
                        if (arrMin[i] != 0)
                        {
                            S1++;
                            A1++;
                        }
                    }
                    Console.Write("jumlah s1 = ");
                    Console.Write(S1);
                    S2 = 0;
                    //A1 = 0;
                    for (i = 0; i < arrMax.Length; i++)
                    {
                        if (arrMax[i] != 0)
                        {
                            S2++;
                       //     A1++;
                        }
                    }
                    Console.WriteLine("jumlah s2 = ");
                    Console.Write(S2);
                    Console.WriteLine("jumlah A1 =");
                    Console.WriteLine(A1);
                    A2 = 0;
                    for (i = 0; i < arrEq.Length; i++)
                    {
                        if (arrEq[i] != 0)
                        {
                            A2++;
                        }
                    }
                    Console.WriteLine("jumlah A2 = ");
                    Console.WriteLine(A2);
                    //jml var tambahan
                    int x;
                    x = 0;
                    for (i = 0; i < constraint; i++)
                    {
                        if (arrMax[i] != 0 && arrMin[i] != 0)
                        {
                            x++;
                        }
                    }
                    Console.WriteLine("jumlah vartambahan = ");
                    Console.Write(x);
                    //tabel awal
                    int VarAll, MtableRow, MtableCol;
                    VarAll = S1 + S2 + A1 + A2;
                    MtableRow = constraint + x;
                    MtableCol = varinit + VarAll;
                    double[,] Mcoef = new double[MtableRow, MtableCol];
                    double[,] Mbcoef = new double[MtableRow, 1];
                    double[] Mccoef = new double[MtableCol];
                    double[,] Mtable = new double[MtableRow + 1, MtableCol + 1];
                    //koefisien fungsi tujuan
                    for (i = 0; i < varinit; i++)
                    {
                        Mccoef[i] = double.Parse(dataGridView1.Rows[i].Cells[1].Value + "");
                    }
                    //cari nilai M
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            if (Mval < coef[i, j])
                            {
                                Mval = coef[i, j];
                            }
                        }
                    }

                    for (i = 0; i < varinit; i++)
                    {
                        if (Mval < double.Parse(dataGridView1.Rows[i].Cells[1].Value + ""))
                        {
                            Mval = double.Parse(dataGridView1.Rows[i].Cells[1].Value + "");
                        }

                    }
                    for (i = 0; i < arrMax.Length; i++)
                    {
                        if (Mval < arrMax[i])
                        {
                            Mval = arrMax[i];
                        }
                    }
                    for (i = 0; i < arrMin.Length; i++)
                    {
                        if (Mval < arrMin[i])
                        {
                            Mval = arrMin[i];
                        }
                    }
                    for (i = 0; i < arrEq.Length; i++)
                    {
                        if (Mval < arrEq[i])
                        {
                            Mval = arrEq[i];
                        }
                    }
                    for (i = 0; i < varinit; i++)
                    {
                        if (Mval < Mccoef[i])
                        {
                            Mval = Mccoef[i];
                        }
                    }
                    Mval = -3 * Mval;
                    Console.Write("var M : ");
                    Console.WriteLine(Mval);
                    //isi tabel awal dgn 0
                    for (i = 0; i < MtableRow; i++)
                    {
                        for (j = 0; j < MtableCol; j++)
                        {
                            Mcoef[i, j] = 0;
                            Console.Write(Mcoef[i, j]);
                        }
                        Console.WriteLine();
                    }
                    //isi per DG
                    //max
                    Console.WriteLine("isi dg baru");
                    int a, b, c;
                    a = 0;
                    for (i = 0; i < arrMax.Length; i++)
                    {
                        if (arrMax[i] != 0)
                        {
                            for (j = 0; j < varinit; j++)
                            {
                                Mcoef[a, j] = coef[i, j];
                                Mbcoef[a, 0] = arrMax[i];
                            }
                            a++;
                        }
                    }

                    //tampilkan 
                    Console.WriteLine("max");
                    for (i = 0; i < MtableRow; i++)
                    {
                        for (j = 0; j < MtableCol; j++)
                        {
                            Console.Write(Mcoef[i, j]);
                        }
                        Console.WriteLine();
                    }
                    b = a;
                    Console.Write(b);
                    for (i = 0; i < arrMin.Length; i++)
                    {
                        if (arrMin[i] != 0)
                        {
                            for (j = 0; j < varinit; j++)
                            {
                                Mcoef[b, j] = coef[i, j];
                                Mbcoef[b, 0] = arrMin[i];
                            }
                            b++;
                        }
                    }
                    //tampilkan 
                    Console.WriteLine("min");
                    for (i = 0; i < MtableRow; i++)
                    {
                        for (j = 0; j < MtableCol; j++)
                        {
                            Console.Write(Mcoef[i, j]);
                        }
                        Console.WriteLine();
                    }

                    c = b;
                    for (i = 0; i < arrEq.Length; i++)
                    {
                        if (arrEq[i] != 0)
                        {
                            for (j = 0; j < varinit; j++)
                            {
                                Mcoef[c, j] = coef[i, j];
                                Mbcoef[c, 0] = arrEq[i];
                            }
                            c++;
                        }
                    }
                    //isi dgn 1
                    for (i = 0; i < S2; i++)
                    {
                        for (j = varinit; j < varinit + S2; j++)
                        {
                            Mcoef[i, varinit + i] = 1;
                        }
                    }
                    //tampilkan 
                    Console.WriteLine("isi dgn 1");
                    for (i = 0; i < MtableRow; i++)
                    {
                        for (j = 0; j < MtableCol; j++)
                        {
                            Console.Write(Mcoef[i, j]);
                        }
                        Console.WriteLine();
                    }

                    //isi dgn -1 dan 1
                    int f, g;
                    f = 0;
                    g = 1;
                    for (i = S2; i < S1 + S2; i++)
                    {
                        Mcoef[i, varinit + S2 + f] = -1;
                        Mccoef[varinit + S2 + f] = 0;

                        Mcoef[i, varinit + S2 + g] = 1;
                        Mccoef[varinit + S2 + g] = Mval;

                        f += 2;
                        g += 2;
                    }

                    //isi sisanya dgn 1
                    for (i = S1 + S2; i < MtableRow; i++)
                    {
                        for (j = varinit + S1 + S2 + A1; j < MtableCol; j++)
                        {
                            Mcoef[i, varinit + A1 + i] = 1;
                        }
                    }
                    //tampilkan 
                    Console.WriteLine("isi dgn -1 dan 1");
                    for (i = 0; i < MtableRow; i++)
                    {
                        for (j = 0; j < MtableCol; j++)
                        {
                            Console.Write(Mcoef[i, j]);
                        }
                        Console.WriteLine();
                    }
                    Console.WriteLine("bcoef");
                    for (i = 0; i < MtableRow; i++)
                    {
                        Console.Write(Mbcoef[i, 0]);

                    }

                    //koefisien fungsi tujuan
                    //for (i = 0; i < varinit; i++)
                    //{
                    //  Mccoef[i] = double.Parse(dataGridView1.Rows[i].Cells[1].Value + "");
                    //}
                    for (i = varinit; i < varinit + S2; i++)
                    {
                        Mccoef[i] = 0;
                    }
                    for (i = MtableCol - A2; i < MtableCol; i++)
                    {
                        Mccoef[i] = Mval;
                    }
                    //mccoef
                    Console.WriteLine("nilai Mccoef: ");
                    for (i = 0; i < MtableCol; i++)
                    {
                        Console.Write(Mccoef[i]);
                    }
                    //gabungkan menjadi Mtable
                    Mtable[0, 0] = 0;
                    for (i = 1; i < Mccoef.Length + 1; i++)
                    {
                        Mtable[0, i] = Mccoef[i - 1];
                    }
                    for (i = 1; i < Mbcoef.Length + 1; i++)
                    {
                        Mtable[i, 0] = Mbcoef[i - 1, 0];
                    }
                    for (i = 1; i < Mbcoef.Length + 1; i++)
                    {
                        for (j = 1; j < Mccoef.Length + 1; j++)
                        {
                            Mtable[i, j] = Mcoef[i - 1, j - 1];
                        }
                    }
                    Console.WriteLine("mtable: ");
                    //tampilkan mtable
                    for (i = 0; i < MtableRow + 1; i++)
                    {
                        for (j = 0; j < MtableCol + 1; j++)
                        {
                            Console.Write(Mtable[i, j]);
                        }
                        Console.WriteLine("");
                    }
                    Mesin.InnerProduct(ref Mtable, A1, S2, A2, Mval);
                    Console.WriteLine("mtable baru: ");
                    //tampilkan mtable
                    for (i = 0; i < MtableRow + 1; i++)
                    {
                        for (j = 0; j < MtableCol + 1; j++)
                        {
                            Console.Write(Mtable[i, j]);
                            Console.Write(" - ");
                        }
                        Console.WriteLine();
                    }
                    Mesin.CariMax(Mtable);
                    Console.WriteLine("mtable hasil search min: ");
                    //tampilkan mtable
                    for (i = 0; i < MtableRow + 1; i++)
                    {
                        for (j = 0; j < MtableCol + 1; j++)
                        {
                            Console.Write(Mtable[i, j]);
                            Console.Write(" ");
                        }
                        Console.WriteLine("");
                    }
                    Console.WriteLine("nilai fungsi objektif: ");
                    Console.Write(Mtable[0, 0]);
                }
                #endregion
            }
            #endregion

            #region Button Minimasi

            if (!toGetMax)
            {
                #region Simplex Min

                if (simMax == constraint && simMin == 0 && simEq == 0)
                {
                    //matriks awal

                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            coef[i, j] = double.Parse(dataGridView4.Rows[i].Cells[j + 1].Value + "");
                            Console.Write(coef[i, j]);
                        }
                        Console.WriteLine(" awal");
                    }

                    for (i = 0; i < constraint; i++)
                        for (j = varinit; j < vartotal; j++)
                        {
                            coef[i, varinit + i] = 1;
                            Console.WriteLine(coef[i, j]);
                        }
                    Console.WriteLine("tambah 1");
                    //nilai kanan jika smua constraint memiliki batas maksimum
                    for (i = 0; i < constraint; i++)
                    {
                        bcoef[i, 0] = double.Parse(dataGridView2.Rows[i].Cells[2].Value + "");

                    }

                    //koefisien fungsi tujuan
                    for (j = 0; j < varinit; j++)
                    {
                        ccoef[j] = double.Parse(dataGridView1.Rows[j].Cells[1].Value + "");
                    }
                    for (j = varinit; j < vartotal; j++)
                    {
                        ccoef[j] = 0;
                    }

                    //menghasilkan tabel
                    table[0, 0] = 0;
                    for (i = 1; i < vartotal + 1; i++)
                    {
                        table[0, i] = ccoef[i - 1];
                    }
                    for (i = 1; i < constraint + 1; i++)
                    {
                        table[i, 0] = bcoef[i - 1, 0];
                    }
                    for (i = 1; i < constraint + 1; i++)
                    {
                        for (j = 1; j < vartotal + 1; j++)
                        {
                            table[i, j] = coef[i - 1, j - 1];
                        }
                    }

                    //matriks awal
                    for (i = 0; i < constraint+1; i++)
                    {
                        for (j = 0; j < vartotal+1; j++)
                        {
                            Console.Write(table[i, j]);
                        }
                        Console.WriteLine("tabel");
                    }
                    Mesin.CariMax(table);

                    //hasil
                    for (i = 0; i < constraint + 1; i++)
                    {
                        for (j = 0; j < vartotal + 1; j++)
                            Console.Write("{0,5}  ", table[i, j].ToString());
                        Console.WriteLine();
                    }
                    Console.WriteLine("{0} nilai fungsi objective", table[0, 0]);

                }
                #endregion

                #region Dual Min

                else if (simMin == constraint && simMax == 0 && simEq == 0)
                {
                    //matriks awal
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            coefd[i, j] = double.Parse(dataGridView4.Rows[i].Cells[j + 1].Value + "");
                            Console.Write(coefd[i, j]);
                        }
                        Console.WriteLine(" matrik awal");
                    }
                    //transpose
                    tklm = coefd.GetLength(0);
                    Console.WriteLine(tklm);
                    tbrs = coefd.GetLength(1);
                    Console.WriteLine(tbrs);

                    //transpose matrikx
                    for (i = 0; i < tklm; i++)
                    {
                        for (j = 0; j < tbrs; j++)
                        {
                            tcoef[j, i] = coefd[i, j];
                        }
                    }
                    Console.WriteLine(" matriks transpose");

                    //tambah coef dg 0
                    for (i = 0; i < varinit; i++)
                        for (j = constraint; j < vartotal; j++)
                        {
                            tcoef[i, j] = 0;

                        }
                    //var s
                    for (i = 0; i < varinit; i++)
                    {
                        for (j = constraint; j < vartotal; j++)
                        {
                            tcoef[i, constraint + i] = 1;
                        }
                    }

                    //matriks kendala
                    for (i = 0; i < varinit; i++)
                    {
                        for (j = 0; j < vartotal; j++)
                        {
                            Console.Write(tcoef[i, j]);
                        }
                        Console.WriteLine();
                    }
                    //nilai kanan diambil dr fungsi kendala
                    for (i = 0; i < varinit; i++)
                    {
                        tbcoef[i, 0] = double.Parse(dataGridView1.Rows[i].Cells[1].Value + "");
                    }

                    //koefisien fungsi tujuan
                    for (j = 0; j < constraint; j++)
                    {
                        tccoef[j] = double.Parse(dataGridView2.Rows[j].Cells[1].Value + "");
                    }
                    for (j = constraint; j < vartotal; j++)
                    {
                        tccoef[j] = 0;
                    }

                    //menghasilkan tabel
                    ttable[0, 0] = 0;
                    for (i = 1; i < vartotal + 1; i++)
                    {
                        ttable[0, i] = tccoef[i - 1];
                    }
                    for (i = 1; i < varinit + 1; i++)
                    {
                        ttable[i, 0] = tbcoef[i - 1, 0];
                    }
                    for (i = 1; i < varinit + 1; i++)
                    {
                        for (j = 1; j < vartotal + 1; j++)
                        {
                            ttable[i, j] = tcoef[i - 1, j - 1];

                        }

                    }
                    //tabel awal
                    for (i = 0; i < varinit + 1; i++)
                    {
                        for (j = 0; j < vartotal + 1; j++)
                        {
                            Console.Write(ttable[i, j]);
                        }
                        Console.WriteLine(" hasil");
                    }

                    Mesin.CariMin(ttable);
                    //hasil
                    for (i = 0; i < varinit + 1; i++)
                    {
                        for (j = 0; j < vartotal + 1; j++)
                            Console.Write("{0,5}  ", ttable[i, j].ToString());
                        Console.WriteLine();
                    }
                    Console.WriteLine("{0} nilai fungsi objective", -ttable[0, 0]);

                }

                #endregion

                #region MMin

                else
                {
                    //Msimplex min
                    //coef awal & max value
                    double Mval;
                    Mval = 0;
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            coef[i, j] = double.Parse(dataGridView4.Rows[i].Cells[j + 1].Value + "");
                            Console.Write(coef[i, j]);
                           
                        }
                        Console.WriteLine(" awal");
                    }
                    for (i = 0; i < varinit; i++)
                    {
                        
                    }
                    Console.WriteLine("koefisien awal c");
                    //isi array dgn 0
                    for (i = 0; i < constraint; i++)
                    {
                        arrMin[i] = 0;
                        arrMax[i] = 0;
                        arrEq[i] = 0;
                    }
                    Console.WriteLine("isi 0");
                    //isi dgn nilai DGview2
                    for (i = 0; i < constraint; i++)
                    {
                        if (Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[2].Value) == string.Empty)
                        {
                            arrMin[i] = double.Parse(dataGridView2.Rows[i].Cells[1].Value + "");
                            Console.Write("min ");
                            Console.Write(arrMin[i]);
                            
                        }
                        if (Convert.ToString(dataGridView2.Rows[i].Cells[2].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) == string.Empty)
                        {
                            arrMax[i] = double.Parse(dataGridView2.Rows[i].Cells[2].Value + "");
                            Console.Write("max ");
                            Console.Write(arrMax[i]);
                        }
                        if (Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[2].Value) != string.Empty)
                        {
                            arrEq[i] = double.Parse(dataGridView2.Rows[i].Cells[2].Value + "");
                            Console.Write("Eq ");
                            Console.Write(arrEq[i]);
                        }
                    }
                    //count s1,s2,A1
                    S1 = 0;
                    A1 = 0;
                    for (i = 0; i < arrMin.Length; i++)
                    {
                        if (arrMin[i] != 0)
                        {
                            S1++;
                            A1++;
                        }
                    }
                    Console.Write("jumlah s1 = ");
                    Console.Write(S1);
                    S2 = 0;
                    //A1 = 0;
                    for (i = 0; i < arrMax.Length; i++)
                    {
                        if (arrMax[i] != 0)
                        {
                            S2++;
                            //A1++;
                        }
                    }
                    Console.WriteLine("jumlah s2 = ");
                    Console.Write(S2);
                    Console.WriteLine("jumlah A1 =");
                    Console.WriteLine(A1);
                    A2 = 0;
                    for (i = 0; i < arrEq.Length; i++)
                    {
                        if (arrEq[i] != 0)
                        {
                            A2++;
                        }
                    }
                    Console.WriteLine("jumlah A2 = ");
                    Console.WriteLine(A2);
                    //jml var tambahan
                    int x;
                    x = 0;
                    for (i = 0; i < constraint; i++)
                    {
                        if (arrMax[i] != 0 && arrMin[i] != 0)
                        {
                            x++;
                        }
                    }
                    Console.WriteLine("jumlah vartambahan = ");
                    Console.Write(x);
                    //tabel awal
                    int VarAll, MtableRow, MtableCol;
                    VarAll = S1 + S2 + A1 + A2;
                    MtableRow = constraint + x;
                    MtableCol = varinit + VarAll;
                    double[,] Mcoef = new double[MtableRow, MtableCol];
                    double[,] Mbcoef = new double[MtableRow, 1];
                    double[] Mccoef = new double[MtableCol];
                    double[,] Mtable = new double[MtableRow + 1, MtableCol + 1];
                    //koefisien fungsi tujuan
                    for (i = 0; i < varinit; i++)
                    {
                        Mccoef[i] = double.Parse(dataGridView1.Rows[i].Cells[1].Value + "");
                    }
                    //cari nilai M
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            if (Mval < coef[i, j])
                            {
                                Mval = coef[i, j];
                            }
                        }
                    }
                   
                    for (i = 0; i < varinit; i++)
                    {
                        if (Mval < double.Parse(dataGridView1.Rows[i].Cells[1].Value + ""))
                        {
                            Mval = double.Parse(dataGridView1.Rows[i].Cells[1].Value + "");
                        }
                        
                    }
                    for (i = 0; i < arrMax.Length; i++)
                    {
                        if (Mval < arrMax[i])
                        {
                            Mval = arrMax[i];
                        }
                    }
                    for (i = 0; i < arrMin.Length; i++)
                    {
                        if (Mval < arrMin[i])
                        {
                            Mval = arrMin[i];
                        }
                    }
                    for (i = 0; i < arrEq.Length; i++)
                    {
                        if (Mval < arrEq[i])
                        {
                            Mval = arrEq[i];
                        }
                    }
                    for (i = 0; i < varinit; i++)
                    {
                        if (Mval < Mccoef[i])
                        {
                            Mval = Mccoef[i];
                        }
                    }
                    Mval = 3 * Mval;
                    Console.Write("var M : ");
                    Console.WriteLine(Mval);
                    //isi tabel awal dgn 0
                    for (i = 0; i < MtableRow; i++)
                    {
                        for (j = 0; j < MtableCol; j++)
                        {
                            Mcoef[i, j] = 0;
                            Console.Write(Mcoef[i, j]);
                        }
                        Console.WriteLine();
                    }
                    //isi per DG
                    //max
                    Console.WriteLine("isi dg baru");
                    int a, b, c;
                    a = 0;
                    for (i = 0; i < arrMax.Length; i++)
                    {
                        if (arrMax[i] != 0)
                        {
                            for (j = 0; j < varinit; j++)
                            {
                                Mcoef[a, j] = coef[i, j];
                                Mbcoef[a, 0] = arrMax[i];
                            }
                            a++;
                        }
                    }

                    //tampilkan 
                    Console.WriteLine("max");
                    for (i = 0; i < MtableRow; i++)
                    {
                        for (j = 0; j < MtableCol; j++)
                        {
                            Console.Write(Mcoef[i, j]);
                        }
                        Console.WriteLine();
                    }
                    b = a;
                    Console.Write(b);
                    for (i = 0; i < arrMin.Length; i++)
                    {
                        if (arrMin[i] != 0)
                        {
                            for (j = 0; j < varinit; j++)
                            {
                                Mcoef[b, j] = coef[i, j];
                                Mbcoef[b, 0] = arrMin[i];
                            }
                            b++;
                        }
                    }
                    //tampilkan 
                    Console.WriteLine("min");
                    for (i = 0; i < MtableRow; i++)
                    {
                        for (j = 0; j < MtableCol; j++)
                        {
                            Console.Write(Mcoef[i, j]);
                        }
                        Console.WriteLine();
                    }

                    c = b;
                    for (i = 0; i < arrEq.Length; i++)
                    {
                        if (arrEq[i] != 0)
                        {
                            for (j = 0; j < varinit; j++)
                            {
                                Mcoef[c, j] = coef[i, j];
                                Mbcoef[c, 0] = arrEq[i];
                            }
                            c++;
                        }
                    }
                    //isi dgn 1
                    for (i = 0; i < S2; i++)
                    {
                        for (j = varinit; j < varinit + S2; j++)
                        {
                            Mcoef[i, varinit + i] = 1;
                        }
                    }
                    //tampilkan 
                    Console.WriteLine("isi dgn 1");
                    for (i = 0; i < MtableRow; i++)
                    {
                        for (j = 0; j < MtableCol; j++)
                        {
                            Console.Write(Mcoef[i, j]);
                        }
                        Console.WriteLine();
                    }

                    //isi dgn -1 dan 1
                    int f, g;
                    f = 0;
                    g = 1;
                    for (i = S2; i < S1 + S2; i++)
                    {
                        Mcoef[i, varinit + S2 + f] = -1;
                        Mccoef[varinit + S2 + f] = 0;

                        Mcoef[i, varinit + S2 + g] = 1;
                        Mccoef[varinit + S2 + g] = Mval;

                        f += 2;
                        g += 2;
                    }

                    //isi sisanya dgn 1
                    for (i = S1 + S2; i < MtableRow; i++)
                    {
                        for (j = varinit + S1 + S2 + A1; j < MtableCol; j++)
                        {
                            Mcoef[i, varinit + A1 + i] = 1;
                        }
                    }
                    //tampilkan 
                    Console.WriteLine("isi dgn -1 dan 1");
                    for (i = 0; i < MtableRow; i++)
                    {
                        for (j = 0; j < MtableCol; j++)
                        {
                            Console.Write(Mcoef[i, j]);
                        }
                        Console.WriteLine();
                    }
                    Console.WriteLine("bcoef");
                    for (i = 0; i < MtableRow; i++)
                    {
                        Console.Write(Mbcoef[i, 0]);

                    }

                    //koefisien fungsi tujuan
                    //for (i = 0; i < varinit; i++)
                    //{
                      //  Mccoef[i] = double.Parse(dataGridView1.Rows[i].Cells[1].Value + "");
                    //}
                    for (i = varinit; i < varinit + S2; i++)
                    {
                        Mccoef[i] = 0;
                    }
                    for (i = MtableCol - A2; i < MtableCol; i++)
                    {
                        Mccoef[i] = Mval;
                    }
                    //mccoef
                    Console.WriteLine("nilai Mccoef: ");
                    for (i = 0; i < MtableCol; i++)
                    {
                        Console.Write(Mccoef[i]);
                    }
                    //gabungkan menjadi Mtable
                    Mtable[0, 0] = 0;
                    for (i = 1; i < Mccoef.Length + 1; i++)
                    {
                        Mtable[0, i] = Mccoef[i - 1];
                    }
                    for (i = 1; i < Mbcoef.Length + 1; i++)
                    {
                        Mtable[i, 0] = Mbcoef[i - 1, 0];
                    }
                    for (i = 1; i < Mbcoef.Length + 1; i++)
                    {
                        for (j = 1; j < Mccoef.Length + 1; j++)
                        {
                            Mtable[i, j] = Mcoef[i - 1, j - 1];
                        }
                    }
                    Console.WriteLine("mtable: ");
                    //tampilkan mtable
                    for (i = 0; i < MtableRow + 1; i++)
                    {
                        for (j = 0; j < MtableCol + 1; j++)
                        {
                            Console.Write(Mtable[i, j]);
                        }
                        Console.WriteLine("");
                    }
                    Mesin.InnerProduct(ref Mtable, A1, S2, A2, Mval);
                    Console.WriteLine("mtable baru: ");
                    //tampilkan mtable
                    for (i = 0; i < MtableRow + 1; i++)
                    {
                        for (j = 0; j < MtableCol + 1; j++)
                        {
                            Console.Write(Mtable[i, j]);
                            Console.Write(" - ");
                        }
                        Console.WriteLine();
                    }
                    Mesin.CariMin(Mtable);
                    Console.WriteLine("mtable hasil search min: ");
                    //tampilkan mtable
                    for (i = 0; i < MtableRow + 1; i++)
                    {
                        for (j = 0; j < MtableCol + 1; j++)
                        {
                            Console.Write(Mtable[i, j]);
                            Console.Write(" ");
                        }
                        Console.WriteLine("");
                    }
                    Console.WriteLine("nilai fungsi objektif: ");
                    Console.Write(Mtable[0, 0]);
                }
                #endregion
            }

            #endregion

        }
    }
}
